package com.example.ssm.basic.mybatis;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 这个类中可以自定义一些数据库操作方法
 */
//@Component
public class SelfDefSqlSessionTemplate extends SqlSessionTemplate {

//    @Autowired
    public SelfDefSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        super(sqlSessionFactory);
    }

/*
    public Page<?> page(String statement,Object param,PageHelper<?> pageMes){

    }

 */

    public <T> T mySelectOne(String statement,String dbType,Object param){
        String[] splits = statement.split("\\.");
        String[] withDb = Arrays.copyOf(splits,splits.length + 1);
        withDb[withDb.length - 2] = dbType;
        withDb[withDb.length - 1] = splits[splits.length - 1];
        String statementWithDb = String.join(".", withDb);
        return selectOne(statementWithDb,param);
    }
}
